<!--
Copyright 2014 The Chromium Authors. All rights reserved.
Use of this source code is governed by a BSD-style license that can be
found in the LICENSE file.
-->

<link rel="import" href="../bower_components/paper-button/paper-button.html">
<link rel="import" href="../bower_components/paper-shadow/paper-shadow.html">
<link rel="import" href="../model/tree-status.html">
<link rel="import" href="ct-failure-stream.html">
<link rel="import" href="ct-last-updated.html">
<link rel="import" href="ct-party-time.html">
<link rel="import" href="ct-tree-status.html">

<polymer-element name="ct-unexpected-failures" attributes="tree repositories failures">
  <template>
    <style>
      .toolbar {
        display: flex;
        justify-content: space-between;
        align-items: baseline;
        padding: 0 5px;
      }
      paper-shadow {
        margin: 1em;
        padding: 1em;
        font-size: large;
        font-weight: bold;
        background: white;
        text-align: center;
      }
    </style>
    <ct-tree-status status="{{ treeStatuses['chromium'] }}" state="{{ treeStatuses['chromium'].status }}"></ct-tree-status>
    <template if="{{ tree == 'blink' }}">
      <ct-tree-status status="{{ treeStatuses['blink'] }}" state="{{ treeStatuses['blink'].status }}"></ct-tree-status>
    </template>
    <template if="{{ tree == 'trooper' }}">
      <paper-shadow z="1">
        <div class="trooper-bugs">
          <a href="https://code.google.com/p/chromium/issues/list?can=2&q=Infra%3DTroopers">Current Trooper bug list</a>
        </div>
      </paper-shadow>
    </template>
    <template if="{{ failures && failures.failures && (tree != 'health' && !failures.failures[tree] || !failures.failures[tree].length) }}">
      <ct-party-time></ct-party-time>
    </template>
    <ct-failure-stream caption="Tree closers" category="treeCloser" groups="{{ failures && failures.failures[tree] }}" repositories="{{ repositories }}"></ct-failure-stream>
    <ct-failure-stream caption="Probably-hung bots" category="builder" groups="{{ failures && failures.failures[tree] }}" repositories="{{ repositories }}"></ct-failure-stream>
    <ct-failure-stream caption="Reliable failures" category="default" groups="{{ failures && failures.failures[tree] }}" repositories="{{ repositories }}"></ct-failure-stream>
    <ct-failure-stream caption="Failures that have only happened once (on one bot)" category="failedOnce" groups="{{ failures && failures.failures[tree] }}" repositories="{{ repositories }}"></ct-failure-stream>
    <ct-failure-stream caption="Snoozed failures" category="snoozed" groups="{{ failures && failures.failures[tree] }}" repositories="{{ repositories }}"></ct-failure-stream>
  </template>
  <script>
  (function() {
    Polymer({
      tree: '',

      created: function() {
        this.treeStatuses = {};
        var projects = ['chromium', 'blink'];
        for (var i = 0; i < projects.length; i++) {
          this.treeStatuses[projects[i]] = new TreeStatus(projects[i]);
        }
      },

      attached: function() {
        this.update();
      },

      update: function() {
        // FIXME: These shouldn't update if there's already an update in progress.
        // This causes noticeable jank becuse <tree-status>.update makes an xhr
        // for current?format=json every time you nav from one tree to another.
        Object.keys(this.treeStatuses, function(tree, status) {
          status.update();
        });
      },
    });
  })();
  </script>
</polymer-element>
